/**
 * utils for encrypt and decrypt
 * the utils supply md5Encode base64Encode base64Decode and so on
 * by huilin corp.ltd @author lms 2016.07.05
 */
package com.ipfsemc.ipfsemc.wx;


import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;

@Component
public class EncryptTool {

    @Autowired
    private StringUtil stringUtil;          /* 字符串处理类 */

    public final String MD5 = "MD5";
    public final String SHA1 = "SHA1";

    /**
     * MD5、SHA1等加密算法(不可逆) <br>
     * 1、生成一个指定加密方式的加密计算摘要 <br>
     * 2、计算(指定加密方式)函数 <br>
     * 3、digest()最后确定返回md5hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符<br>
     * 4、BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值<br>
     *
     * @param value 需要加密的内容
     * @param type  加密方式
     * @return 加密后的字符串(MD5 ： 32 、 SHA1 ： 40)
     */
    public String encryption(String value, String type) {
        try {
            MessageDigest md5 = null;
            md5 = MessageDigest.getInstance(type);
            byte[] byteArray = value.getBytes("UTF-8");
            byte[] md5Bytes = md5.digest(byteArray);
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = md5Bytes[i] & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * base64Encode
     *
     * @param str the string to be encode
     * @return "" when error occur,otherwise return encode string
     * @author lms 2016.07.05
     */
    public String base64Encode(String str) {
        if ((str == null) || (str.equals(""))) {
            return "";
        }
        try {
            byte[] buf = str.getBytes("UTF-8");
            Class<?> clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
            Method mainMethod = clazz.getMethod("encode", byte[].class);
            mainMethod.setAccessible(true);
            Object retObj = mainMethod.invoke(null, new Object[]{buf});
            return (String) retObj;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * base64Decode
     *
     * @param encodeStr
     * @return decode string
     * @author lms 2016.07.05
     */
    public String base64Decode(String encodeStr) {
        try {
            Class<?> clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
            Method mainMethod = clazz.getMethod("decode", String.class);
            mainMethod.setAccessible(true);
            byte[] retObj = (byte[]) mainMethod.invoke(null, encodeStr);
            return new String(retObj, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * to encode str by encode style
     *
     * @param str to be encoded string
     * @param enc encode style,such as "UTF-8"/"UTF8","GBK"
     * @return encoded string
     */
    public String httpEncode(String str, String enc) {
        String ret = "";
        if (stringUtil.isEmpty(str)) {
            return ret;
        }
        if (stringUtil.isEmpty(enc)) {
            enc = "UTF-8";
        }
        try {
            ret = URLEncoder.encode(str, enc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * to decode str by encode style
     *
     * @param str to be encoded string
     * @param enc encode style,such as "UTF-8"/"UTF8","GBK"
     * @return encoded string
     */
    public String httpDecode(String str, String enc) {
        String ret = "";
        try {
            ret = URLDecoder.decode(str, enc);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ret;
    }



}
